package org.one.stone.soup.grfxML.plugin.text;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.one.stone.soup.grfxML.DataSize;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.DataText;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;

public class TrailToText extends SimplePlugin {
	DataString file = new DataString();
	DataText target = new DataText();
	DataSize maxData = new DataSize();
	DataState load = new DataState();
/**
 * FileToText constructor comment.
 */
public TrailToText(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize() {}
/**
 * process method comment.
 */
public void process()
{
 try{
	if(load.getValue()==true)
	{
		String fileName = file.getValue();

		byte[] data = org.one.stone.soup.file.FileHelper.readTail( fileName,maxData.getValue() );

		BufferedReader reader = new BufferedReader( new InputStreamReader(new org.one.stone.soup.stringhelper.StringInputStream( new String(data) ) ) );

		target.setEmpty();

		try{
			String line = reader.readLine();
			while(line!=null)
			{
				target.add( new DataString(line),this );
				line = reader.readLine();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			load.setValue(false,this);
			return;
		}

		try{
			reader.close();
		}catch(Exception e){}

		load.setValue(false,this);
	}
 }catch(LockException le)
 {}
}
/**
 * register method comment.
 */
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	file = grfxMLCaster.cast(file,getArg(file,0,store));
	load = grfxMLCaster.cast(load,getArg(load,1,store));
	target = grfxMLCaster.cast(target,getArg(target,2,store));
	maxData = grfxMLCaster.cast(maxData,getArg(maxData,3,store));
}
public void replace(Field oldObj, Field newObj)
{
	file = grfxMLCaster.replace(file,oldObj,newObj);
	load = grfxMLCaster.replace(load,oldObj,newObj);
	target = grfxMLCaster.replace(target,oldObj,newObj);
	maxData = grfxMLCaster.replace(maxData,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
